package com.yds.android_librariesall

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import pub.devrel.easypermissions.EasyPermissions
import android.Manifest
import android.util.Log
import pub.devrel.easypermissions.AppSettingsDialog

class MainActivity : AppCompatActivity(), EasyPermissions.PermissionCallbacks {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)

        checkPermission()
    }

    private fun checkPermission() {
        val perms = arrayOf(
            Manifest.permission.CAMERA,
        )
        if (EasyPermissions.hasPermissions(this, *perms)) {
            Log.i("YDSimon", "已经拥有权限，执行相应操作")

        } else {
            // 没有权限，请求权限
            EasyPermissions.requestPermissions(this, "需要相机权限", 100, *perms)
        }
    }

    override fun onPermissionsGranted(requestCode: Int, perms: List<String?>) {
        // 已经拥有权限，执行相应操作·
        Log.i("YDSimon", "已经拥有权限，执行相应操作")
    }

    override fun onPermissionsDenied(requestCode: Int, perms: List<String?>) {
        // 权限被拒绝
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            AppSettingsDialog.Builder(this).build().show()
        }
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String?>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        // 将结果转发给 EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

}